﻿namespace AC.ExtendedRenderer.Toolkit
{
    using ComponentFactory.Krypton.Toolkit;
    using Microsoft.VisualBasic;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Text;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms;

    public class KryptonSlider : UserControl, IContentValues
    {
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;
        private IContainer components;
        internal KryptonSliderButton kminus;
        internal KryptonSliderButton kplus;
        private bool m_down;
        private int m_fireInterval = 200;
        private bool m_highlight;
        private IDisposable m_mementoBack1;
        private IDisposable m_mementoBack2;
        private IDisposable m_mementoContent;
        private PaletteBackInheritRedirect m_paletteBack;
        private PaletteBorderInheritRedirect m_paletteBorder;
        private PaletteContentInheritRedirect m_paletteContent;
        private int m_range = 100;
        private bool m_singleClick;
        private bool m_sliderhighlight;
        private Color m_sliderlinebottom = Color.FromArgb(0xde, 0xe2, 0xec);
        private Color m_sliderlinetop = Color.FromArgb(0x74, 150, 0xc2);
        private int m_step = 2;
        private int m_value;

        public event ValueChangedEventHandler ValueChanged;

        public KryptonSlider()
        {
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            this.InitializeComponent();
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this.m_paletteBack = new PaletteBackInheritRedirect(this._paletteRedirect);
            this.m_paletteBorder = new PaletteBorderInheritRedirect(this._paletteRedirect);
            this.m_paletteContent = new PaletteContentInheritRedirect(this._paletteRedirect);
            this.BackColor = Color.Transparent;
        }

        private void ChangeValue(int NewValue)
        {
            if (base.Enabled)
            {
                if (this.ValueChanged != null)
                {
                    this.ValueChanged(this, new SliderEventArgs(NewValue, this.m_value, this.m_range, this.m_step));
                }
                this.m_value = NewValue;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.m_mementoContent != null)
                {
                    this.m_mementoContent.Dispose();
                    this.m_mementoContent = null;
                }
                if (this.m_mementoBack1 != null)
                {
                    this.m_mementoBack1.Dispose();
                    this.m_mementoBack1 = null;
                }
                if (this.m_mementoBack2 != null)
                {
                    this.m_mementoBack2.Dispose();
                    this.m_mementoBack2 = null;
                }
                if (this.components != null)
                {
                    this.components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void DrawLines(Graphics Gfx)
        {
            Gfx.SmoothingMode = SmoothingMode.AntiAlias;
            Gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Pen pen = new Pen(this.m_sliderlinetop, 1f);
            Pen pen2 = new Pen(this.m_sliderlinebottom, 1f);
            Gfx.DrawLine(pen, 0x11, (base.Height / 2) - 1, base.Width - 0x12, (base.Height / 2) - 1);
            Gfx.DrawLine(pen2, 0x11, base.Height / 2, base.Width - 0x12, base.Height / 2);
            Gfx.DrawLine(pen, (int) (base.Width / 2), (int) ((base.Height / 2) - 3), (int) (base.Width / 2), (int) ((base.Height / 2) + 3));
            Gfx.DrawLine(pen2, (int) ((base.Width / 2) + 1), (int) ((base.Height / 2) - 3), (int) ((base.Width / 2) + 1), (int) ((base.Height / 2) + 3));
        }

        private void DrawSlider(Graphics Gfx)
        {
            Gfx.SmoothingMode = SmoothingMode.AntiAlias;
            Gfx.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            Rectangle sliderBounds = this.GetSliderBounds(this.GetSliderPosition());
            if (this._palette != null)
            {
                IRenderer renderer = this._palette.GetRenderer();
                using (RenderContext context = new RenderContext(this, Gfx, sliderBounds, renderer))
                {
                    using (GraphicsPath path = this.GetSliderPath(this.GetSliderPosition()))
                    {
                        this.m_paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                        this.m_mementoBack1 = renderer.RenderStandardBack.DrawBack(context, sliderBounds, path, this.m_paletteBack, VisualOrientation.Top, base.Enabled ? PaletteState.Normal : PaletteState.Disabled, this.m_mementoBack1);
                    }
                    PaletteState sliderState = this.GetSliderState();
                    this.m_paletteBack.Style = PaletteBackStyle.ButtonStandalone;
                    this.m_paletteBorder.Style = PaletteBorderStyle.ButtonStandalone;
                    this.m_paletteContent.Style = PaletteContentStyle.ButtonStandalone;
                    if (this.m_paletteBack.GetBackDraw(sliderState) == InheritBool.True)
                    {
                        using (GraphicsPath path2 = this.GetSliderPath(this.GetSliderPosition()))
                        {
                            this.m_mementoBack2 = renderer.RenderStandardBack.DrawBack(context, sliderBounds, path2, this.m_paletteBack, VisualOrientation.Top, sliderState, this.m_mementoBack2);
                            Gfx.DrawPath(new Pen(this.m_paletteBorder.GetBorderColor2(sliderState)), path2);
                            Gfx.DrawLine(new Pen(this.m_paletteBorder.GetBorderColor2(sliderState)), (int) ((sliderBounds.X + (sliderBounds.Width / 2)) + 1), (int) (sliderBounds.Y + 4), (int) ((sliderBounds.X + (sliderBounds.Width / 2)) + 1), (int) (sliderBounds.Y + 8));
                        }
                    }
                }
            }
        }

        public Image GetImage(PaletteState state)
        {
            return null;
        }

        public Color GetImageTransparentColor(PaletteState state)
        {
            return Color.Empty;
        }

        public string GetLongText()
        {
            return string.Empty;
        }

        private Point GetPointFromValue(int Value)
        {
            if (Value == 0)
            {
                return new Point((int) Math.Round((double) (((double) base.Width) / 2.0)), (int) Math.Round((double) ((((double) base.Height) / 2.0) - 6.5)));
            }
            Point point = new Point((int) Math.Round((double) ((((double) base.Width) / 2.0) + (((((double) base.Width) / 2.0) - 20.0) * (((double) Value) / (((double) this.m_range) / 2.0))))), (int) Math.Round((double) ((((double) base.Height) / 2.0) - 6.5)));
            if (point.X > (base.Width - 0x16))
            {
                point.X = base.Width - 0x16;
            }
            if (point.X < 20)
            {
                point.X = 20;
            }
            return point;
        }

        public string GetShortText()
        {
            return string.Empty;
        }

        private Rectangle GetSliderBounds(Point Location)
        {
            return new Rectangle(Location.X, Location.Y, 9, 13);
        }

        private GraphicsPath GetSliderPath(Point Location)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddLine(new Point(Location.X + 1, Location.Y), new Point(Location.X + 1, Location.Y + 9));
            path.AddLine(new Point(Location.X + 1, Location.Y + 9), new Point(Location.X + 5, Location.Y + 13));
            path.AddLine(new Point(Location.X + 5, Location.Y + 13), new Point(Location.X + 9, Location.Y + 9));
            path.AddLine(new Point(Location.X + 9, Location.Y + 9), new Point(Location.X + 9, Location.Y));
            path.AddLine(new Point(Location.X + 1, Location.Y), new Point(Location.X + 9, Location.Y));
            return path;
        }

        private Point GetSliderPosition()
        {
            return new Point((int) Math.Round((double) (this.GetPointFromValue(this.m_value).X - 4.5)), (int) Math.Round((double) ((((double) base.Height) / 2.0) - 6.5)));
        }

        private RangeTests GetSliderRangeTest(Point Location)
        {
            if ((Location.X > 20) & (Location.X < (base.Width - 20)))
            {
                if ((Location.X >= ((((double) base.Width) / 2.0) - 5.0)) & (Location.X <= ((((double) base.Width) / 2.0) + 5.0)))
                {
                    return RangeTests.MiddleDomain;
                }
                if (Location.X < (((double) base.Width) / 2.0))
                {
                    return RangeTests.LeftDomain;
                }
                return RangeTests.RightDomain;
            }
            if (Location.X <= 20)
            {
                return RangeTests.MinusDomain;
            }
            if (Location.X >= (base.Width - 20))
            {
                return RangeTests.PlusDomain;
            }
            return RangeTests.ElseDomain;
        }

        private PaletteState GetSliderState()
        {
            if (!base.Enabled)
            {
                return PaletteState.Disabled;
            }
            if (this.m_down)
            {
                return PaletteState.Pressed;
            }
            if (!this.m_highlight)
            {
                return PaletteState.Normal;
            }
            if (this.m_sliderhighlight)
            {
                return PaletteState.CheckedTracking;
            }
            return PaletteState.Tracking;
        }

        private int GetValueFromPoint(Point Value)
        {
            int num = (int) Math.Round((double) ((((Value.X - (((double) base.Width) / 2.0)) / ((((double) base.Width) / 2.0) - 20.0)) * this.m_range) / 2.0));
            if (num > (((double) this.m_range) / 2.0))
            {
                num = (int) Math.Round((double) (((double) this.m_range) / 2.0));
            }
            if (num < ((((double) this.m_range) / 2.0) * -1.0))
            {
                num = (int) Math.Round((double) ((((double) this.m_range) / 2.0) * -1.0));
            }
            return num;
        }

        private void InitColors()
        {
            this.m_sliderlinetop = this._palette.ColorTable.GripDark;
            this.m_sliderlinebottom = this._palette.ColorTable.GripLight;
        }

        private void InitializeComponent()
        {
            this.kplus = new KryptonSliderButton();
            this.kminus = new KryptonSliderButton();
            base.SuspendLayout();
            this.kplus.Anchor = AnchorStyles.Right;
            this.kplus.BackColor = Color.Transparent;
            this.kplus.ButtonStyle = KryptonSliderButton.ButtonStyles.PlusButton;
            this.kplus.EventFireRate = 200;
            this.kplus.Font = new Font("Segoe UI", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.kplus.Location = new Point(0x7c, 0);
            this.kplus.Name = "kplus";
            this.kplus.Orientation = VisualOrientation.Top;
            this.kplus.SingleClick = false;
            this.kplus.Size = new Size(0x10, 0x10);
            this.kplus.TabIndex = 1;
            this.kplus.VisualLook = PaletteBackStyle.ButtonStandalone;
            this.kplus.SliderButtonFire += new KryptonSliderButton.SliderButtonFireEventHandler(this.kplus_SliderButtonFire);
            this.kminus.Anchor = AnchorStyles.Left;
            this.kminus.BackColor = Color.Transparent;
            this.kminus.ButtonStyle = KryptonSliderButton.ButtonStyles.MinusButton;
            this.kminus.EventFireRate = 200;
            this.kminus.Font = new Font("Segoe UI", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            this.kminus.Location = new Point(0, 0);
            this.kminus.Name = "kminus";
            this.kminus.Orientation = VisualOrientation.Top;
            this.kminus.SingleClick = false;
            this.kminus.Size = new Size(0x10, 0x10);
            this.kminus.TabIndex = 0;
            this.kminus.VisualLook = PaletteBackStyle.ButtonStandalone;
            this.kminus.SliderButtonFire += new KryptonSliderButton.SliderButtonFireEventHandler(this.kminus_SliderButtonFire);
            base.AutoScaleMode = AutoScaleMode.None;
            this.BackColor = Color.Transparent;
            base.Controls.Add(this.kplus);
            base.Controls.Add(this.kminus);
            this.DoubleBuffered = true;
            this.Font = new Font("Segoe UI", 8.25f, FontStyle.Regular, GraphicsUnit.Point, 0);
            base.Name = "KryptonSlider";
            base.Size = new Size(140, 0x10);
            base.MouseLeave += new System.EventHandler(this.KryptonSliderButton_MouseLeave);
            base.MouseMove += new MouseEventHandler(this.KryptonSlider_MouseMove);
            base.MouseDown += new MouseEventHandler(this.KryptonSliderButton_MouseDown);
            base.MouseUp += new MouseEventHandler(this.KryptonSliderButton_MouseUp);
            base.MouseEnter += new System.EventHandler(this.KryptonSliderButton_MouseEnter);
            base.ResumeLayout(false);
        }

        private void k_palette_BasePaletteChanged(object sender, EventArgs e)
        {
            base.Invalidate();
        }

        private void k_palette_BaseRendererChanged(object sender, EventArgs e)
        {
            base.Invalidate();
        }

        private void k_palette_PalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        private void kminus_SliderButtonFire(KryptonSliderButton Sender, EventArgs e)
        {
            this.SliderDecrement();
            base.Invalidate();
        }

        private void kplus_SliderButtonFire(KryptonSliderButton Sender, EventArgs e)
        {
            this.SliderIncrement();
            base.Invalidate();
        }

        private void KryptonSlider_MouseMove(object sender, MouseEventArgs e)
        {
            bool flag = false;
            if (this.GetSliderBounds(this.GetSliderPosition()).Contains(e.Location))
            {
                if (!this.m_sliderhighlight)
                {
                    this.m_sliderhighlight = true;
                    flag = true;
                }
            }
            else if (this.m_sliderhighlight)
            {
                this.m_sliderhighlight = false;
                flag = true;
            }
            if (this.m_down)
            {
                this.ChangeValue(this.GetValueFromPoint(e.Location));
                flag = true;
            }
            if (flag)
            {
                base.Invalidate();
            }
        }

        private void KryptonSliderButton_MouseDown(object sender, MouseEventArgs e)
        {
            if (this.GetSliderBounds(this.GetSliderPosition()).Contains(e.Location))
            {
                this.m_down = true;
                base.Invalidate();
            }
        }

        private void KryptonSliderButton_MouseEnter(object sender, EventArgs e)
        {
            this.m_highlight = true;
            base.Invalidate();
        }

        private void KryptonSliderButton_MouseLeave(object sender, EventArgs e)
        {
            this.m_highlight = false;
            base.Invalidate();
        }

        private void KryptonSliderButton_MouseUp(object sender, MouseEventArgs e)
        {
            this.m_down = false;
            if (!this.GetSliderBounds(this.GetSliderPosition()).Contains(e.Location))
            {
                switch (this.GetSliderRangeTest(e.Location))
                {
                    case RangeTests.LeftDomain:
                        this.ChangeValue(this.GetValueFromPoint(e.Location));
                        break;

                    case RangeTests.MiddleDomain:
                        this.ChangeValue(0);
                        break;

                    case RangeTests.RightDomain:
                        this.ChangeValue(this.GetValueFromPoint(e.Location));
                        break;
                }
            }
            base.Invalidate();
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                this.InitColors();
            }
            base.Invalidate();
        }

        protected override void OnLayout(LayoutEventArgs e)
        {
            if (this._palette != null)
            {
                PaletteState sliderState = this.GetSliderState();
                Rectangle sliderBounds = this.GetSliderBounds(this.GetSliderPosition());
                IRenderer renderer = this._palette.GetRenderer();
                using (ViewLayoutContext context = new ViewLayoutContext(this, renderer))
                {
                    this.m_paletteContent.Style = PaletteContentStyle.ButtonStandalone;
                    if (this.m_mementoContent != null)
                    {
                        this.m_mementoContent.Dispose();
                    }
                    this.m_mementoContent = renderer.RenderStandardContent.LayoutContent(context, sliderBounds, this.m_paletteContent, this, VisualOrientation.Top, sliderState, false);
                }
            }
            base.OnLayout(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.DrawLines(e.Graphics);
            this.DrawSlider(e.Graphics);
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        private void SliderDecrement()
        {
            if ((this.m_value - this.m_step) >= ((this.m_range / 2) * -1))
            {
                this.ChangeValue(this.m_value - this.m_step);
            }
        }

        private void SliderIncrement()
        {
            if ((this.m_value + this.m_step) <= (this.m_range / 2))
            {
                this.ChangeValue(this.m_value + this.m_step);
            }
        }

        public int FireInterval
        {
            get
            {
                return this.m_fireInterval;
            }
            set
            {
                this.m_fireInterval = value;
                this.kplus.EventFireRate = value;
                this.kminus.EventFireRate = value;
                base.Invalidate();
            }
        }

        public int Maximum
        {
            get
            {
                return (this.m_range / 2);
            }
        }

        public int Minimum
        {
            get
            {
                return ((-1 * this.m_range) / 2);
            }
        }

        public int Range
        {
            get
            {
                return this.m_range;
            }
            set
            {
                if (this.m_range != value)
                {
                    this.m_range = value;
                    base.Invalidate();
                }
            }
        }

        public bool SingleClick
        {
            get
            {
                return this.m_singleClick;
            }
            set
            {
                this.m_singleClick = value;
                this.kplus.SingleClick = value;
                this.kminus.SingleClick = value;
                base.Invalidate();
            }
        }

        public int Steps
        {
            get
            {
                return this.m_step;
            }
            set
            {
                if (this.m_step != value)
                {
                    this.m_step = value;
                    base.Invalidate();
                }
            }
        }

        public int Value
        {
            get
            {
                return this.m_value;
            }
            set
            {
                if (this.m_value != value)
                {
                    this.m_value = value;
                    base.Invalidate();
                }
            }
        }

        public enum RangeTests
        {
            MinusDomain,
            LeftDomain,
            MiddleDomain,
            RightDomain,
            PlusDomain,
            ElseDomain
        }

        public class SliderEventArgs : EventArgs
        {
            private int m_newvalue;
            private int m_oldvalue;
            private int m_range;
            private int m_steps;

            public SliderEventArgs(int NewValue, int OldValue, int Range, int Steps)
            {
                this.m_newvalue = NewValue;
                this.m_oldvalue = OldValue;
                this.m_range = Range;
                this.m_steps = Steps;
            }

            public override string ToString()
            {
                string str = "";
                object obj2 = str + "Values: " + DateAndTime.Now.ToLongTimeString() + "\r\n";
                object obj3 = string.Concat(new object[] { obj2, "NewValue: ", this.m_newvalue, "\r\n" });
                object obj4 = string.Concat(new object[] { obj3, "OldValue: ", this.m_oldvalue, "\r\n" });
                object obj5 = string.Concat(new object[] { obj4, "Range: ", this.m_range, "\r\n" });
                return string.Concat(new object[] { obj5, "Steps: ", this.m_steps, "\r\n" });
            }

            public int NewValue
            {
                get
                {
                    return this.m_newvalue;
                }
            }

            public int OldValue
            {
                get
                {
                    return this.m_oldvalue;
                }
            }

            public int Range
            {
                get
                {
                    return this.m_range;
                }
            }

            public int Steps
            {
                get
                {
                    return this.m_steps;
                }
            }
        }

        public delegate void ValueChangedEventHandler(KryptonSlider Sender, KryptonSlider.SliderEventArgs e);
    }
}

